<template>
  <div class="my-container">
    <!-- 登录状态 -->
    <div v-if="user" class="header userInfo">
      <div class="baseInfo">
        <div class="left">
          <van-image
            class="avatar"
            round
            :src="userInfo.photo"
          />
          <span class="name">{{userInfo.name}}</span>
        </div>
        <div class="right">
          <van-button to="/user/profile" type="default" round size="mini">编辑资料</van-button>
        </div>
      </div>
      <div class="infoData">
        <div class="dataItem">
          <span class="count">{{userInfo.art_count}}</span>
          <span class="text">头条</span>
        </div>
        <div class="dataItem">
          <span class="count">{{userInfo.follow_count}}</span>
          <span class="text">关注</span>
        </div>
        <div class="dataItem">
          <span class="count">{{userInfo.fans_count}}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="dataItem">
          <span class="count">{{userInfo.like_count}}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>
    <!-- 未登录状态 -->
    <div v-else class="header not-login">
      <div class="loginBox" @click="$router.push('/login')">
        <img src="@/assets/mobile.png" alt="" />
        <span>登录 / 注册</span>
      </div>
    </div>

    <!-- grid格 -->
    <van-grid :column-num="2" class="gridNav" clickable>
      <van-grid-item>
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item>
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>

    <!-- cell -->
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <van-cell v-if="user" title="退出登录" class="logout" @click="onLogout" />
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import { getUserInfo } from "@/api/user.js";
export default {
  name: "myIndex",
  data() {
    return {
      userInfo: {},
    };
  },
  methods: {
    ...mapMutations(["setUser"]),

    onLogout() {
      this.$dialog
        .confirm({
          title: "确定要退出吗",
        })
        .then(() => {
          //将vuex中的user设置为空，vuex中再将localStorage中设置为空
          this.setUser(null);
          console.log("ok");
        })
        .catch(() => {
          // on cancel
          //   console.log("err");
        });
    },
    async loadUserInfo() {
      try {
        const {data:res} = await getUserInfo();
        // console.log(res);
        this.userInfo = res.data
      } catch (err) {
        return this.$toast.fail("获取用户信息失败");
      }
    },
  },
  created() {
    if (this.user) {
      this.loadUserInfo();
    }
  },
  computed: {
    ...mapState(["user"]),
  },
};
</script>

<style lang="less" scoped>
.my-container {
  .header {
    height: 361px;
    background: url("~@/assets/banner.png");
    background-size: cover;
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .loginBox {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      img {
        width: 132px;
        height: 132px;
        margin-bottom: 15px;
      }
      span {
        font-size: 28px;
        color: #fff;
      }
    }
  }
  .userInfo {
    .baseInfo {
      height: 231px;
      //   background-color: pink;
      padding: 75px 30px 30px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
          margin-right: 15px;
          border: 3px solid #fff;
        }
        .name {
          font-size: 30px;
          color: #fff;
        }
      }
    }
    .infoData {
      //   background-color: gray;
      display: flex;

      .dataItem {
        height: 130px;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }

  .gridNav {
    height: 141px;
    margin-bottom: 10px;
    .toutiao {
      font-size: 45px;
    }
    .toutiao-shoucang {
      color: #eb5253;
    }
    .toutiao-lishi {
      color: #ff9d1d;
    }
    .text {
      font-size: 28px;
    }
  }
  .logout {
    margin-top: 10px;
    text-align: center;
    color: #eb5253;
    font-size: 30px;
  }
}
</style>